package com.jp.knowledge.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.adapter.AttentionAdapter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLine;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.TabHostWrapPagerView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SearchFragment extends BaseFragment implements ResultSubscriber.OnResultListener {
    private View mView;
    private final int NAV_LIST_CODE = 0x000001;//导航列表
    private final int HEAD_DATA_CODE = 0x000002;//头部信息

    @ViewInject(R.id.them_tab_nav)
    private RecyclerView recycleView;

    @ViewInject(R.id.view_page)
    private TabHostWrapPagerView pager;

    private String infoId;
    private int type;

    private List<HeadLine> headLines;
    private AttentionAdapter attentAdapter;

    private int lastPosition;//上一个选中位置

    private String[] itemNames = new String[]{"头条", "主题", "体验", "产品", "公司", "人物", "团队圈", "文档", "文库", "知识库"};

    private int[] types = new int[]{1, 9, 10, 8, 3, 4, 11,0,0,0};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.search_fragment, null);
        x.view().inject(this, mView);
        init();
        return mView;
    }

    protected void init() {
        getHeadLines();
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        attentAdapter = new AttentionAdapter(headLines, mContext);
        recycleView.setLayoutManager(manager);
        recycleView.setAdapter(attentAdapter);
        recycleView.setItemAnimator(new DefaultItemAnimator());
        setFragment();
    }

    private void getHeadLines() {
        int size = itemNames.length;
        List<HeadLine> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            HeadLine headLine = new HeadLine();
            headLine.setTypeName(itemNames[i]);
            headLine.setTypeId(types[i] + "");
            list.add(headLine);
        }
        headLines=list;
    }
    private void setFragment(){
        pager.setOffscreenPageLimit(1);
        if (headLines != null && headLines.size() > 0) {
            headLines.get(0).setCheck(true);//默认选中第一个
            attentAdapter.update(headLines);
            int size = headLines.size();
            List<Fragment> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                SearchDetailFragment fragment = new SearchDetailFragment();
                fragment.setLabel(headLines.get(i).getTypeId());
                fragment.setType(types[i]);
                fragment.setInfoId(infoId);
                list.add(fragment);
            }
            pager.init(recycleView, attentAdapter, getFragmentManager(), list);
        }
    }

    /**
     * 获取 导航
     */
    private void getHeadNav() {
        JsonObject param = new JsonObject();
        param.addProperty("type", type);
        HttpHelper.getInstance(mContext).subscribeNavList(param, NAV_LIST_CODE, this);
    }


    @Override
    public void onStart(int requestType) {

        loadingDialog.show();

    }

    @Override
    public void onCompleted(int requestType) {
        loadingDialog.cancel();

    }

    @Override
    public void onError(int requestType) {
        loadingDialog.cancel();
    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code = data.getErrcode();
        if (requestType == NAV_LIST_CODE) {
            if (code == 0) {
                headLines = JsonUtil.getInstance().listMapToObject((ArrayList<LinkedTreeMap<String, Object>>) data.getData(), HeadLine.class);
                if (headLines != null && headLines.size() > 0) {
                    headLines.get(0).setCheck(true);//默认选中第一个
                    attentAdapter.update(headLines);
                    int size = headLines.size();
                    List<Fragment> list = new ArrayList<>();
                    for (int i = 0; i < size; i++) {
                        AttentionDetailFragment fragment = new AttentionDetailFragment();
                        fragment.setLabel(headLines.get(i).getTypeId());
                        fragment.setType(type);
                        fragment.setInfoId(infoId);
                        list.add(fragment);
                    }
                    pager.init(recycleView, attentAdapter, getFragmentManager(), list);
                } else {
                    ToasUtil.toast(mContext, "获取导航数据失败");
                }

            } else {

            }
        }
    }
}
